﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace xiaoxiong.KYJControllersLib
{
    /// <summary>
    /// 所有窗体的枚举
    /// </summary>
    public enum FormNames
    {
        控制流程,
        日志报警,
        临界窗体,
        日志查询,
        参数设置,
        实时趋势,
        历史趋势,
        数据报表,
        用户管理,
        退出系统
    }

    public partial class NaviPanel : UserControl
    {
        public NaviPanel()
        {
            InitializeComponent();

            BindEvent();
        }

        [Browsable(true)]
        [Category("自定义事件")]
        [Description("按钮选择切换事件")]
        public event Func<object, EventArgs, bool> ButtonSelectEvent;


        /// <summary>
        /// 按钮选择通用事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_ButtonSelect_Click(object sender, EventArgs e)
        {
            if (sender is Button button)
            {
                if (Enum.IsDefined(typeof(FormNames), button.Text))
                {
                    FormNames fromNames = (FormNames)Enum.Parse(typeof(FormNames), button.Text);

                    if (ButtonSelectEvent != null && ButtonSelectEvent(fromNames,e) == true)
                    {
                        //改变对象按钮颜色
                        SetButton(fromNames);
                    }
                }
            }
        }

        /// <summary>
        /// 通过代码绑定
        /// </summary>
        private void BindEvent()
        {
            foreach (var item in this.panelEnhanced1.Controls)
            {
                if (item is Button button)
                {
                    if (Enum.IsDefined(typeof(FormNames),button.Text))
                    {
                        button.Click += btn_ButtonSelect_Click;
                    }
                }
            }
        }

        /// <summary>
        /// 设置按钮状态
        /// </summary>
        /// <param name="formNames"></param>
        private void SetButton(FormNames formNames)
        {
            foreach (var item in this.panelEnhanced1.Controls)
            {
                if (item is Button button)
                {
                    if (button.Text == formNames.ToString())
                    {
                        button.Font = new System.Drawing.Font("微软雅黑", 10.5F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
                        button.ForeColor = System.Drawing.Color.Cyan;
                    }
                    else
                    {
                        button.Font = new System.Drawing.Font("微软雅黑", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
                        button.ForeColor = System.Drawing.Color.Transparent;
                    }
                }
            }
        }
    }
}
